home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Games / Glypha II 1.21 / source / Code ƒ / J-Glypha.p < prev    next >
Encoding:
Text File  |  1993-07-01  |  11.8 KB  |  387 lines  |  [TEXT/PJMM]

  1. program Glypha;    {Glypha II 1991 Soft Dorothy}
  2.  
  3. {  This code is for all to disect and laugh at.  It is fairly      }
  4. {  sloppy and very, ver underdocumented.  Still, I have no time    }
  5. {  to go back and improve upon this code, so I offer it as is.     }
  6. {  It nonetheless reflects the abilities of one in the middle of   }
  7. {  learning how to write a game in Pascal on the Mac.  Almost      }
  8. {  every function can be improved - if only to make each function  }
  9. {  more elegant.  There are no overwhelming "bugs" though.  Still, }
  10. {  for example, you shouldn't write to your own resource fork as   }
  11. {  this game does - and if you decide to write to your resource    }
  12. {  fork anyway, you could do it more elegantly than I did here.    }
  13.  
  14. {$I-}
  15.  
  16.     uses
  17.         Sound, Palettes, Globals, GameUtils, Enemies, GlyphaGuts, Initialize, Menus;
  18.  
  19.     var
  20.         code, index, theMenu, theItem, chCode, flashes: integer;
  21.         ch: char;
  22.         mResult, tickWait, dummyLong: LongInt;
  23.         whichWindow: WindowPtr;
  24.         tempRect: Rect;
  25.         theInput: TEHandle;
  26.         mousePt: Point;
  27.         err: OSErr;
  28.         eventHappened: Boolean;
  29.  
  30. {===================================}
  31.  
  32.     procedure CheckTheMouse;
  33.         var
  34.             screenPos: Integer;
  35.  
  36. {--------------------}
  37.  
  38.         procedure HorizontalMouse;
  39.             const
  40.                 MBState = $172;
  41.                 MTemp = $828;
  42.                 RawMouse = $82C;
  43.                 Mouse = $830;
  44.                 CrsrNew = $8CE;
  45.                 CrsrCouple = $8CF;
  46.                 Couple = $FF;
  47.                 Uncouple = $00;
  48.             var
  49.                 center: Point;
  50.                 lowGlob: Integer;
  51.                 lowMem: Ptr;
  52.                 pointPtr: ^Point;
  53.         begin
  54.             SetPt(center, mousePt.h, 240);
  55.             lowMem := Pointer(rawMouse);
  56.             pointPtr := @lowMem^;
  57.             pointPtr^ := center;
  58.             lowMem := Pointer(MTemp);
  59.             pointPtr := @lowMem^;
  60.             pointPtr^ := center;
  61.             lowMem := Pointer(CrsrNew);
  62.             lowMem^ := $FFFF;
  63.         end;
  64.  
  65. {--------------------}
  66.  
  67.     begin
  68.         GetMouse(mousePt);
  69.         HorizontalMouse;
  70.         screenPos := (mousePt.h - 320 - rightOffset) div 8;
  71.         if (screenPos > 16) then
  72.             screenPos := 16;
  73.         if (screenPos < -16) then
  74.             screenPos := -16;
  75.  
  76.         with thePlayer do
  77.             begin
  78.                 if (facing = 0) then
  79.                     begin
  80.                         if (screenPos < 0) then
  81.                             facing := 1
  82.                         else
  83.                             begin
  84.                                 if (screenPos > horiVel) then
  85.                                     keyStillDown := TRUE
  86.                                 else
  87.                                     keyStillDown := FALSE;
  88.                             end;
  89.                     end
  90.                 else
  91.                     begin
  92.                         if (screenPos > 0) then
  93.                             facing := 0
  94.                         else
  95.                             begin
  96.                                 if (screenPos < horiVel) then
  97.                                     keyStillDown := TRUE
  98.                                 else
  99.                                     keyStillDown := FALSE;
  100.                             end;
  101.                     end;
  102.             end;
  103.     end;
  104.  
  105. {===================================}
  106.  
  107.     procedure CheckTheKeyboard;
  108.         var
  109.             keyState: KeyMap;
  110.     begin
  111.         keyStillDown := FALSE;
  112.         GetKeys(keyState);
  113.  
  114.         if ((keyState[kLeftKey1]) or (keyState[kLeftKey2])) then
  115.             begin
  116.                 keyStillDown := TRUE;
  117.                 thePlayer.facing := kLeftFace;
  118.             end;
  119.         if ((keyState[kRightKey1]) or (keyState[kRightKey2])) then
  120.             begin
  121.                 keyStillDown := TRUE;
  122.                 thePlayer.facing := kRightFace;
  123.             end;
  124.     end;
  125.  
  126. {===================================}
  127.  
  128.     procedure HandleGameEvent;
  129.         var
  130.             wasPort: GrafPtr;
  131.     begin
  132.         case theEvent.what of
  133.             KeyDown: 
  134.                 begin
  135.                     chCode := BitAnd(theEvent.message, CharCodeMask);
  136.                     if (ODD(theEvent.modifiers div CmdKey)) then
  137.                         begin
  138.                             mResult := MenuKey(CHR(chCode));
  139.                             theMenu := HiWord(mResult);
  140.                             theItem := LoWord(mResult);
  141.                             if (theMenu <> 0) then
  142.                                 Handle_My_Menu(theMenu, theItem, theInput); {Do the menu selection}
  143.                         end
  144.                     else if (keyboardControl) then
  145.                         with thePlayer do
  146.                             case chCode of
  147.                                 kSpaceBar:        {flap}
  148.                                     begin
  149.                                         state := TRUE;
  150.                                     end;
  151.                                 82, 114:                    {refresh}
  152.                                     begin
  153.                                         CopyBits(BitMapPtr(virginCPtr^.portPixMap^)^, mainWndo^.portBits, wholeArea, wholeArea, srcCopy, mainWndo^.visRgn);
  154.                                         ShowScore;
  155.                                         ShowMortals;
  156.                                         ShowLevel;
  157.                                     end;
  158.                                 70, 102:                    {flush}
  159.                                     begin
  160.                                         GetPort(GrafPtr(wasPort));
  161.                                         SetPort(GrafPtr(mainWndo));
  162.                                         FillRect(mainWndo^.portBits.bounds, black);
  163.                                         SetPort(GrafPtr(wasPort));
  164.                                     end;
  165.  
  166.                                 otherwise
  167.                                     begin
  168.                                     end;
  169.                             end;    {end case chCode of}
  170.                 end;        {end of KeyDown event}
  171.             MouseDown: 
  172.                 begin
  173.                     thePlayer.state := TRUE;
  174.                 end;
  175.             UpDateEvt:                {Update event for a window}
  176.                 begin                        {Handle the update}
  177.                     whichWindow := WindowPtr(theEvent.message); {Get the window the update is for}
  178.                     if (whichWindow = mainWndo) then
  179.                         begin
  180.                             SetPort(GrafPtr(mainWndo));
  181.                             BeginUpdate(mainWndo);     {Set the clipping to the update area}
  182.                             CopyBits(BitMapPtr(virginCPtr^.portPixMap^)^, mainWndo^.portBits, wholeArea, wholeArea, srcCopy, mainWndo^.visRgn);
  183.                             ShowScore;
  184.                             ShowMortals;
  185.                             ShowLevel;
  186.                             EndUpdate(mainWndo);       {Return to normal clipping area}
  187.                             SetPort(GrafPtr(virginCPtr));
  188.                         end;
  189.                 end;
  190.             otherwise
  191.         end;
  192.     end;
  193.  
  194. {===================================}
  195.  
  196. begin
  197.     InitVariables;
  198.     UnloadSeg(@InitVariables);
  199.     tickWait := TickCount;
  200.     theInput := nil;
  201.  
  202.     repeat
  203.  
  204.         CopyBits(BitMapPtr(virginCPtr^.portPixMap^)^, BitMapPtr(loadCPtr^.portPixMap^)^, flameRect[0], flameRect[0], srcCopy, nil);
  205.         CopyBits(BitMapPtr(virginCPtr^.portPixMap^)^, BitMapPtr(virginCPtr^.portPixMap^)^, flameRect[1], flameRect[0], srcCopy, nil);
  206.         CopyBits(BitMapPtr(loadCPtr^.portPixMap^)^, BitMapPtr(virginCPtr^.portPixMap^)^, flameRect[0], flameRect[1], srcCopy, nil);
  207.         CopyBits(BitMapPtr(virginCPtr^.portPixMap^)^, mainWndo^.portBits, flameRect[0], flameRect[0], srcCopy, mainWndo^.visRgn);
  208.         CopyBits(BitMapPtr(virginCPtr^.portPixMap^)^, mainWndo^.portBits, flameRect[1], flameRect[1], srcCopy, mainWndo^.visRgn);
  209.         Delay(4, dummyLong);
  210.         if DoRandom(200) = 0 then
  211.             begin
  212.                 SetPort(GrafPtr(mainWndo));
  213.                 flashes := DoRandom(4) + 1;
  214.                 CopyMask(BitMapPtr(objectCPtr^.portPixMap^)^, offMaskMap, mainWndo^.portBits, eyeRects[4], eyeRects[4], theEye.dest);
  215.                 DoTheSound('lightning.snd', highPriority);
  216.                 for index := 1 to flashes do
  217.                     StrikeLightning(upperEye);
  218.                 CopyBits(BitMapPtr(virginCPtr^.portPixMap^)^, mainWndo^.portBits, theEye.dest, theEye.dest, srcCopy, playRgn);
  219.             end;
  220.  
  221.         if (hasWNE) then
  222.             eventHappened := WaitNextEvent(everyEvent, theEvent, sleep, nil)
  223.         else
  224.             begin
  225.                 SystemTask;
  226.                 eventHappened := GetNextEvent(everyEvent, theEvent);
  227.             end;
  228.  
  229.         if (eventHappened) then
  230.             begin
  231.                 code := FindWindow(theEvent.where, whichWindow); {Get which window the event happened in}
  232.                 case theEvent.what of                            {Decide type of event}
  233.                     MouseDown:                                                {Mouse button pressed}
  234.                         begin                                                        {Handle the pressed button}
  235.                             if (code = inMenuBar) then        {See if a menu selection}
  236.                                 begin                                    {Get the menu selection and handle it    }
  237.                                     mResult := MenuSelect(theEvent.Where);                {Do menu selection    }
  238.                                     theMenu := HiWord(mResult);            {Get the menu list number            }
  239.                                     theItem := LoWord(mResult);            {Get the menu list item number    }
  240.                                     Handle_My_Menu(theMenu, theItem, theInput);         {Handle the menu    }
  241.                                 end;                                            {End of inMenuBar                    }
  242.                             if (code = inSysWindow) then                {See if a DA selection        }
  243.                                 SystemClick(theEvent, whichWindow); {Let other programs in        }
  244.                         end;                                                {End of MouseDown            }
  245.                     KeyDown:                                            {Handle key inputs            }
  246.                         begin
  247.                             with theEvent do
  248.                                 begin
  249.                                     chCode := BitAnd(message, CharCodeMask);    {Get character}
  250.                                     ch := CHR(chCode);                    {Change to ASCII}
  251.                                     if (Odd(modifiers div CmdKey)) then        {See if Command key is down}
  252.                                         begin
  253.                                             mResult := MenuKey(ch);           {See if menu selection}
  254.                                             theMenu := HiWord(mResult); {Get the menu list number}
  255.                                             theItem := LoWord(mResult); {Get the menu item number}
  256.                                             if (theMenu <> 0) then        {See if a list was selected}
  257.                                                 Handle_My_Menu(theMenu, theItem, theInput); {Do the menu selection}
  258.                                         end
  259.                                     else
  260.                                         case ch of
  261.                                             'l': 
  262.                                                 begin
  263.                                                     SetPort(GrafPtr(mainWndo));
  264.                                                     for index := 1 to 10 do
  265.                                                         StrikeLightning(DoRandom(4));
  266.                                                 end;
  267.                                             'L': 
  268.                                                 begin
  269.                                                     SetPort(GrafPtr(mainWndo));
  270.                                                     flashes := DoRandom(4) + 1;
  271.                                                     CopyMask(BitMapPtr(objectCPtr^.portPixMap^)^, offMaskMap, mainWndo^.portBits, eyeRects[4], eyeRects[4], theEye.dest);
  272.                                                     DoTheSound('lightning.snd', highPriority);
  273.                                                     for index := 1 to flashes do
  274.                                                         StrikeLightning(upperEye);
  275.                                                     CopyBits(BitMapPtr(virginCPtr^.portPixMap^)^, mainWndo^.portBits, theEye.dest, theEye.dest, srcCopy, playRgn);
  276.                                                 end;
  277.                                             'm', 'M': 
  278.                                                 DoTheSound('music.snd', highPriority);
  279.                                             otherwise
  280.                                                 begin
  281.                                                 end;
  282.                                         end;
  283.                                 end;                {End for with}
  284.                         end;                        {End for KeyDown,AutoKey}
  285.                     UpDateEvt:                {Update event for a window}
  286.                         begin                        {Handle the update}
  287.                             whichWindow := WindowPtr(theEvent.message); {Get the window the update is for}
  288.                             BeginUpdate(whichWindow);     {Set the clipping to the update area}
  289.                             CopyBits(BitMapPtr(virginCPtr^.portPixMap^)^, mainWndo^.portBits, wholeArea, wholeArea, srcCopy, mainWndo^.visRgn);
  290.                             ShowScore;
  291.                             ShowMortals;
  292.                             ShowLevel;
  293.                             EndUpdate(whichWindow);       {Return to normal clipping area}
  294.                         end;                            {End of UpDateEvt}
  295.                     ActivateEvt:                   {Window activated event}
  296.                         begin                           {Handle the activation}
  297.                             whichWindow := WindowPtr(theEvent.message); {Get the window to be activated}
  298.                             if odd(theEvent.modifiers) then {Make sure it is Activate and not DeActivate}
  299.                                 SelectWindow(whichWindow);    {Activate the window by selecting it}
  300.                         end;                            {End of ActivateEvt}
  301.                     App4Evt: 
  302.                         case BSR(theEvent.message, 24) of    {high byte of message}
  303.                             1:                          {suspendResumeMessage}
  304.                                 if (BitAnd(theEvent.message, suspendResumeBit) = resuming) then
  305.                                     inBackground := FALSE
  306.                                 else
  307.                                     begin
  308.                                         inBackground := TRUE;        {it was a suspend event}
  309.                                         if (chanPtr <> nil) then
  310.                                             err := SndDisposeChannel(chanPtr, TRUE);
  311.                                         chanPtr := nil;
  312.                                     end;
  313.                             otherwise
  314.                                 ;
  315.                         end; {CASE}
  316.                     otherwise
  317.                 end;                              {End of case}
  318.  
  319.                 while (playing) do
  320.                     begin
  321.                         gameCycle := gameCycle + 1;
  322.                         if keyboardControl then
  323.                             CheckTheKeyboard
  324.                         else
  325.                             CheckTheMouse;
  326.                         HideCursor;
  327.                         if (stonesSliding) then
  328.                             SlideTheStones;
  329.                         MoveThePlayer;
  330.                         HandleTheEnemies;
  331.                         UpdateEye;
  332.                         with thePlayer do
  333.                             begin
  334.                                 if ((dest.bottom > handTop - 20) and (dest.left > handLeft) and (not otherState)) then
  335.                                     UpdateTheHand
  336.                                 else if (theHand.state) then
  337.                                     RetractTheHand;
  338.                                 DrawBeasts;
  339.                                 DrawPlayer(dest, oldDest);
  340.                                 oldDest := dest;
  341.                             end;
  342.                         if (deadAndGone) then
  343.                             ExitAMortal;
  344.                         if (onward) then
  345.                             AdvanceALevel;
  346.  
  347.                         repeat
  348.                             if (hasWNE) then
  349.                                 eventHappened := WaitNextEvent(everyEvent, theEvent, 0, nil)
  350.                             else
  351.                                 begin
  352.                                     SystemTask;
  353.                                     eventHappened := GetNextEvent(everyEvent, theEvent);
  354.                                 end;
  355.                             if eventHappened then
  356.                                 HandleGameEvent;
  357.                         until (not pausing);
  358.  
  359.                         repeat
  360.                         until (TickCount >= tickWait);
  361.                         tickWait := TickCount + gameSpeed;
  362.                     end;
  363.             end;
  364.     until doneFlag;                       {End of the event loop}
  365.  
  366.     HUnlock(Handle(playRgn));
  367.     DisposeRgn(playRgn);
  368.     HUnlock(Handle(obeliskRgn1));
  369.     DisposeRgn(obeliskRgn1);
  370.     HUnlock(Handle(obeliskRgn2));
  371.     DisposeRgn(obeliskRgn2);
  372.     if (chanPtr <> nil) then
  373.         err := SndDisposeChannel(chanPtr, FALSE);
  374.  
  375.     DisposePalette(mainPalette);
  376.  
  377.     DisposeWindow(GrafPtr(mainWndo));
  378.     CloseCPort(objectCPtr);
  379.     DisposPtr(objectCBits);
  380.     CloseCPort(virginCPtr);
  381.     DisposPtr(virginCBits);
  382.     CloseCPort(loadCPtr);
  383.     DisposPtr(loadCBits);
  384.  
  385.     WriteOutScores;
  386.  
  387. end.                                    {End of the program}